package com.lrs.s2sh.web.action;

import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.json.annotations.JSON;
import org.apache.struts2.json.annotations.SMDMethod;
import org.springframework.beans.factory.annotation.Autowired;

import com.lrs.s2sh.model.Person;
import com.lrs.s2sh.service.PersonService;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class JsonTestAction extends ActionSupport implements RequestAware {

	private static final long serialVersionUID = 1L;

	private Map<String, Object> request;

	@Autowired
	private PersonService personService;

	private String message = "hello world";
	private String customName = "custom"; 
	private int[] ints = {10, 20};
	private Map<String, String> map = new HashMap<String, String>();
	
	private String field1 = "str";
	// 'transient' fields are not serialized
	private transient String field2;

	// fields without getter method are not serialized
	private String field3;
	
	private String username = null;
	private String password = null;
	
	private Person person;

	public String getMessage() {
		return message;
	}

	@JSON(name="newName")
	public String getCustomName() {
		return customName;
	}

	public void setCustomName(String customName) {
		this.customName = customName;
	}

	public int[] getInts() {
		return ints;
	}

	public void setInts(int[] ints) {
		this.ints = ints;
	}

	public Map<String, String> getMap() {
		return map;
	}

	public void setMap(Map<String, String> map) {
		this.map = map;
	}

	public String getField1() {
		return field1;
	}

	public void setField1(String field1) {
		this.field1 = field1;
	}

	public String getField2() {
		return field2;
	}

	public void setField2(String field2) {
		this.field2 = field2;
	}

	public void setField3(String field3) {
		this.field3 = field3;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public String searchPerson() throws Exception {
		map.put("John", "Galt");

		String userName = (String)request.get("username");
		System.out.println("username=" + userName + ";password=" + this.password);

		person = personService.get(1L);

		return SUCCESS;
	}
	
	public String smd() {
		return Action.SUCCESS;
	}

    @SMDMethod
    public Person doSomething(Person person, int quantity) {
    	person.setFirstName("hello" + quantity);
        return person;
    }
	
	@Override
	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}

}
